public class Solution233 {
    public int countDigitOne(int n) {
        if (n == 0) {
            return 0;
        }
        int res = 0;
        int t = 1;
        while (t <= n / 10) {
            t *= 10;
        }
        if (n / t == 1) {
            res += (n % t + 1);
        } else {
            res += t;
        }
        return res + countDigitOne(t - 1) * (n / t) + countDigitOne(n % t);
    }
}
